#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

#endregion


namespace GameFramework.ScreenSystem
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        /// <param name="musicToPlay">XACT Cue to play</param>
        public MainMenuScreen(string musicToPlay, MenuTransition Transition): base(musicToPlay,Transition)
        {
            MenuEntries.Add("Play Game");
            MenuEntries.Add("Options");
            MenuEntries.Add("Exit");


            // Ensure that the specified audio cue isn't empty or null.
            if(!String.IsNullOrEmpty(musicToPlay))
                GameManager.AudioManager.Play(musicToPlay);
        }
        
        #endregion

        #region Handle Input

        /// <summary>
        /// Responds to user menu selections.
        /// </summary>
        protected override void OnSelectEntry(int entryIndex)
        {

            switch (entryIndex)
            {
                case 0:
                    // Play the game.

                    // Stop all currently playing sounds. 
                    GameManager.AudioManager.StopAllPlaying(); 

                    LoadingScreen.Load(ScreenManager, LoadGameplayScreen, true, "LoadMenuMusic");
                    break;

                case 1:
                    // Go to the options screen.
                    GameManager.AudioManager.StopAllPlaying(); 

                    ScreenManager.AddScreen(new OptionsMenuScreen("OptionsMenuMusic",MenuTransition.Transition4));
                    break;

                case 2:
                    // Exit the sample.

                    // Stop all currently playing sounds. 
                    GameManager.AudioManager.StopAllPlaying();

                    OnCancel();
                    break;
            }
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            // Before returning to the calling code make sure that all sounds started
            // have been stopped.
            GameManager.AudioManager.StopAllPlaying();

            const string message = "Thank you for using XNA GameStudio Express?";

            MessageBoxScreen messageBox = new MessageBoxScreen(message, "QuitMessageBoxMusic");

            messageBox.Accepted += ExitMessageBoxAccepted;

            ScreenManager.AddScreen(messageBox);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        /// <summary>
        /// Loading screen callback for activating the gameplay screen.
        /// </summary>
        void LoadGameplayScreen(object sender, EventArgs e)
        {
            // Add the Gameplay screen to the ScreenManager and use the "PlayScreenMusic"
            // audio music cue as backgroung music.
            ScreenManager.AddScreen(new GameplayScreen("PlayScreenMusic"));
        }
        
        #endregion
    }
}
